package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author げっしょく
 * @date 2021/9/8
 */
public class BrandServiceImpl implements BrandService {
    @Override
    public void insert(Brand brand) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.insert(brand);
        sqlSession.close();
    }

    @Override
    public void delByIdAfter(int id) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.delByIdAfter(id);
    }

    @Override
    public void update(Brand brand) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.update(brand);
    }

    @Override
    public List<Brand> selectAll() {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();
        return brands;
    }

    @Override
    public void delarray(int[] ids) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.delarray(ids);
    }

    @Override
    public List<Brand> selectByPage(int index, int page) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectByPage(index, page);


        return brands;
    }

    @Override
    public int selectCount() {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int i = mapper.selectCount();
        System.out.println(i);
        return i;
    }


    @Override
    public List<Brand> selectByPageAndcond(int index, int page, Brand brand) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectByPageAndcond(index, page, brand);

        return brands;
    }

    @Override
    public int selectCountAndcond(Brand brand) {
        SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int i = mapper.selectCountAndcond(brand);

        return i;
    }
}
